<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class UserDeparment extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'user:department';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $app=\EasyWeChat::work();
       $result= $app->department->list();
       if ($result['errcode']===0){
           foreach ($result['department'] as $item){
               $department=\App\model\UserDepartment::where('department_id',$item['id'])->first();
               if (!$department){
                   $department=new \App\model\UserDepartment();
               }
               $department->department_id=$item['id'];
               $department->name=$item['name'];
               $department->parent_id=$item['parentid'];
               $department->order=$item['order'];
               $department->save();
           }
       }
    }
}
